import axios from 'axios'
import { Message } from 'element-ui'
//项目请求方式均为get请求
const request = (url, params) => {
    const instance = axios.create({
        baseURL: '/api',
        timeout: 5000,
    })
    //请求拦截器
    instance.interceptors.request.use(
        config => {
            return config
        },
        error => {
            console.log(error);
            return Promise.reject(error);
        })
    //响应拦截器
    instance.interceptors.response.use(
        response => {
            const res = response.data
            if ((res.code || res.data.code) !== 200) return Message({
                message: res.message,
                type: 'error',
                duration: 1000
            })
            return res
        },
        error => {
            console.log(error);
            Message({
                message: error.response.data.message,
                type: 'error',
                duration: 1000
            })
            return Promise.reject(error)
        }
    )
    return instance({ url, params })
}

export default request